﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MedicalSystem.SamrtReport.Library;

namespace MedicalSystem.SamrtReport
{
    public partial class DataGridBindingSettingFrm : Form
    {
        public DataGridBindingSettingFrm(DataGridView dataGrid)
        {
            InitializeComponent();
            this._selectedDataGrid = dataGrid;
        }
        private DataGridView _selectedDataGrid;
        private void FDataGridBindingSetting_Load(object sender, EventArgs e)
        {
            Initalize();
            CustomFormHepler.ShowForm(this);
        }
        private DataSet dataSource = new DataSet();
        private void Initalize()
        {
            string fileName = string.Format(@"{0}\{1}.xml", BindingData.Path, BindingData.CurrentBindingSourceName);
            if (File.Exists(fileName))
            {

                dataSource.ReadXmlSchema(fileName);


               
                this.comboBox1.Items.Clear();

                for (int i = 0; i < dataSource.Tables.Count; i++)
                {
                   this.comboBox1.Items.Add(dataSource.Tables[i].TableName);
                       
                }      

            }
            if (this._selectedDataGrid.Columns.Count > 0)
            {
                if (!string.IsNullOrEmpty(this._selectedDataGrid.DataMember))
                    comboBox1.Text = this._selectedDataGrid.DataMember;

                this.listBox1.Items.Clear();

                for (int i = 0; i < this._selectedDataGrid.Columns.Count; i++)
                {
                    ListBoxItem item = new ListBoxItem();
                    item.Text = this._selectedDataGrid.Columns[i].HeaderText;
                    item.Value = this._selectedDataGrid.Columns[i].DataPropertyName;
                    item.Visible = this._selectedDataGrid.Columns[i].Visible;
                    item.Readonly = this._selectedDataGrid.Columns[i].ReadOnly;
                    item.Width = this._selectedDataGrid.Columns[i].Width;
                    item.Index = this._selectedDataGrid.Columns[i].DisplayIndex;
                    this.listBox1.Items.Add(item);
                }

                if (listBox1.Items.Count > 0)
                    this.listBox1.SelectedIndex = 0;
            }
            else
            {
                if( this.comboBox1.Items.Count>0)
                   this.comboBox1.SelectedIndex = 0;
            }
           
        }
    

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(comboBox1.Text))
                return;
            this.listBox1.Items.Clear();
            for (int j = 0; j < dataSource.Tables[comboBox1.Text].Columns.Count; j++)
            {

                  ListBoxItem item = new ListBoxItem();
                  item.Text = dataSource.Tables[comboBox1.Text].Columns[j].Caption;
                  item.Value = dataSource.Tables[comboBox1.Text].Columns[j].ColumnName; ;
                  item.Index = j;
                  this.listBox1.Items.Add(item);                
                    
            }

            if (listBox1.Items.Count > 0)
                this.listBox1.SelectedIndex = 0;
        
        
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null)
                return;

            ListBoxItem item = this.listBox1.SelectedItem as ListBoxItem;
            this.caption.Text = item.Text;
            this.bindingData.Text = item.Value.ToString();
            this.width.Value = item.Width;
            this.index.Value = item.Index;
            this.visible.Checked = !item.Visible;
            this.readonlyTxt.Checked = item.Readonly;
        }

        private void caption_TextChanged(object sender, EventArgs e)
        {
            ListBoxItem item = GetSelectedItem();
            item.Text = this.caption.Text;
        }
        private ListBoxItem GetSelectedItem()
        {
            if (this.listBox1.SelectedItem == null)
                return null;

            ListBoxItem item = this.listBox1.SelectedItem as ListBoxItem;
            return item;
        }
        private void width_ValueChanged(object sender, EventArgs e)
        {
            ListBoxItem item = GetSelectedItem();
            item.Width = Convert.ToInt32(this.width.Value);
        }

        private void visible_CheckedChanged(object sender, EventArgs e)
        {
            ListBoxItem item = GetSelectedItem();
            item.Visible = !this.visible.Checked;
        }

        private void readonlyTxt_CheckedChanged(object sender, EventArgs e)
        {
            ListBoxItem item = GetSelectedItem();
            item.Readonly = this.readonlyTxt.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataGridBindingSetting();
            this.DialogResult = DialogResult.OK;
        }

        private void DataGridBindingSetting()
        {
            this._selectedDataGrid.Columns.Clear();
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                ListBoxItem item = listBox1.Items[i] as ListBoxItem;
                this._selectedDataGrid.Columns.Add(item.Value, item.Text);
                this._selectedDataGrid.Columns[item.Value].DisplayIndex = item.Index;
                this._selectedDataGrid.Columns[item.Value].Visible = item.Visible;
                this._selectedDataGrid.Columns[item.Value].Width = item.Width;
                this._selectedDataGrid.Columns[item.Value].ReadOnly = item.Readonly;
                this._selectedDataGrid.Columns[item.Value].DataPropertyName = item.Value;

            }
            this._selectedDataGrid.DataMember = this.comboBox1.Text;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }

    #region ListBoxItem
     public class ListBoxItem
    {
        private string _text;
        public string Text
        {
            get { return this._text; }
            set { this._text = value; }
        }
        private string _value;
        public string Value
        {
            get { return this._value; }
            set { this._value = value; }
        }
        private int _index;
        public int Index
        {
            get { return this._index; }
            set { this._index = value; }
        }
        private int _width=100;
        public int Width
        {
            get { return this._width; }
            set { this._width = value; }
        }
        private bool _visible=true;
        public bool Visible
        {
            get { return this._visible; }
            set { this._visible = value; }
        }
        private bool _readonly=false;
        public bool Readonly
        {
            get { return this._readonly; }
            set { this._readonly = value; }
        }
    }
    #endregion
}
